package com.project.zsxd.server.common.util;

import cn.hutool.core.util.ObjectUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeUtil {

    private static final DateTimeFormatter yyyy_MM_dd_HH_mm_ss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDateTime parse(String dateTime) {
        return LocalDateTime.parse(dateTime);
    }

    public static String formatDate(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    public static String formatDateTime(LocalDateTime dateTime) {
        return dateTime.format(yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 获取打卡日期
     * @param sleepTime
     * @return
     */
    public static LocalDateTime getSleepDate(LocalDateTime sleepTime) {
        // 当天中午12点
        LocalDateTime middleTime = ObjectUtil.clone(sleepTime);
        middleTime = middleTime.withHour(12).withMinute(0).withSecond(0).withNano(0);
        // 如果入睡时间<中午12点，则记为前一天;否则就是当天
        if (sleepTime.isBefore(middleTime)) {
            sleepTime.minusDays(1);
        }
        return sleepTime;
    }
}
